﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Common.Utility;
using System.Data;
using Runda.EShop.Common.Entity;
using Runda.EShop.Bll;

namespace Runda.EShop.SaaS.CustomerManage
{
    public partial class CustomerProductAdd : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        public Guid CustomerID
        {
            get
            {
                if (Request.QueryString["CustomerID"] != null)
                {
                    return new Guid(Request.QueryString["CustomerID"]);
                }
                return Guid.Empty;
            }
        }

        public string Company
        {
            get
            { return ViewState["_company"].ToString(); }
            set
            { ViewState["_company"] = value; }
        }

        public int EntType
        {
            get
            { return (int)ViewState["_EntType"]; }
            set
            { ViewState["_EntType"] = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["CustomerID"] != null)
                {
                    BizCustomerEntity entity = new CustomerBll().Get<BizCustomerEntity>(CustomerID);
                    BizEnterpriseEntity entityEnt = new BizEnterpriseBll().Get<BizEnterpriseEntity>(entity.EntID);
                    Company = entityEnt.cCO;
                    EntType = entityEnt.EntType;
                }
                if (EntType == (int)EnumHelper.EnterpriseType.Terminal)
                {
                    ddlCateType.DataSource = new BasProductBll().QueryProductCateEx();
                }
                else
                {
                    ddlCateType.DataSource = new BasProductBll().QueryProductCate();
                }
                ddlCateType.DataTextField = "CateName";
                ddlCateType.DataValueField = "CateCode";
                ddlCateType.DataBind();
                ddlCateType.Items.Add(new ListItem("--不限--", ""));
                ddlCateType.SelectedValue = "";


            }
        }

        protected void lnkbtnQuery_Click(object sender, EventArgs e)
        {
            BindGrid(ConstHelper.PAGE_INIT_INDEX);
        }

        private void BindGrid(int PageIndex)
        {

            string strWhereClause = " and Company='" + Company + "'";

            if (!string.IsNullOrEmpty(txtProductName.Text.Trim()))
            {
                strWhereClause += @" and ProductName like '%" + txtProductName.Text.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(txtProductCode.Text.Trim()))
            {
                strWhereClause += @" and ProductCode like '%" + txtProductCode.Text.Trim() + "%'";
            }

            if (!string.IsNullOrEmpty(ddlCateType.SelectedValue) && ddlCateType.SelectedValue != "" && EntType == (int)EnumHelper.EnterpriseType.Agent)
            {
                strWhereClause += @" and CateCode like '" + ddlCateType.SelectedValue + "%'";
            }

            if (!string.IsNullOrEmpty(ddlCateType.SelectedValue) && ddlCateType.SelectedValue != "" && EntType == (int)EnumHelper.EnterpriseType.Terminal)
            {
                strWhereClause += @" and CateCodeEx like '" + ddlCateType.SelectedValue + "%'";
            }

            //List<SqlDbParameter> parms = new List<SqlDbParameter>();
            //SqlDbParameter parm = null;

            //if (!string.IsNullOrEmpty(txtProductName.Text.Trim()))
            //{
            //    parm = new SqlDbParameter();
            //    parm.ColumnName = "ProductName";
            //    parm.ColumnType = DbType.String;
            //    parm.ParameterName = "@ProductName";
            //    parm.ParameterValue = txtProductName.Text.Trim();
            //    parm.QualificationType = SqlDbParameter.QualificationSymbol.Like;
            //    parms.Add(parm);

                

            //}

            //if (!string.IsNullOrEmpty(txtProductCode.Text.Trim()))
            //{
            //    parm = new SqlDbParameter();
            //    parm.ColumnName = "ProductCode";
            //    parm.ColumnType = DbType.String;
            //    parm.ParameterName = "@ProductCode";
            //    parm.ParameterValue = txtProductCode.Text.Trim();
            //    parm.QualificationType = SqlDbParameter.QualificationSymbol.Like;
            //    parms.Add(parm);
            //}
            //if (!string.IsNullOrEmpty(ddlCateType.SelectedValue) && ddlCateType.SelectedValue != "" && EntType == (int)EnumHelper.EnterpriseType.Agent)
            //{
            //    parm = new SqlDbParameter();
            //    parm.ColumnName = "CateCode";
            //    parm.ColumnType = DbType.String;
            //    parm.ParameterName = "@CateCode";
            //    parm.ParameterValue = ddlCateType.SelectedValue;
            //    parm.QualificationType = SqlDbParameter.QualificationSymbol.Like;
            //    parms.Add(parm);
            //}

            //if (!string.IsNullOrEmpty(ddlCateType.SelectedValue) && ddlCateType.SelectedValue != "" && EntType == (int)EnumHelper.EnterpriseType.Terminal)
            //{
            //    parm = new SqlDbParameter();
            //    parm.ColumnName = "CateCodeEx";
            //    parm.ColumnType = DbType.String;
            //    parm.ParameterName = "@CateCodeEx";
            //    parm.ParameterValue = ddlCateType.SelectedValue;
            //    parm.QualificationType = SqlDbParameter.QualificationSymbol.Like;
            //    parms.Add(parm);
            //}

            //parm = new SqlDbParameter();
            //parm.ColumnName = "Company";
            //parm.ColumnType = DbType.String;
            //parm.ParameterName = "@Company";
            //parm.ParameterValue = Company;
            //parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            //parms.Add(parm);

            int RecordCount = 0;
            List<BasProductEntity> list = new SystemBll().GetAll<BasProductEntity>(strWhereClause, "ProductCode DESC", AspNetPager1.PageSize, PageIndex, out RecordCount);

            gvProductList.DataSource = list;
            gvProductList.DataBind();

            AspNetPager1.RecordCount = RecordCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex - 1);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<RelationCustomerProductEntity> list = new List<RelationCustomerProductEntity>();
            RelationCustomerProductEntity entity = null;
            foreach (GridViewRow item in gvProductList.Rows)
            {
                CheckBox cbSelect = item.FindControl("cbSelect") as CheckBox;
                if (cbSelect != null)
                {
                    if (cbSelect.Checked)
                    {
                        entity = new RelationCustomerProductEntity();
                        entity.CustomerID = CustomerID;
                        entity.ProductID = new Guid(gvProductList.DataKeys[item.RowIndex]["ProductID"].ToString());
                        entity.Deleted = false;

                        list.Add(entity);
                    }
                }
            }

            RelationCustomerProductBll bll = new RelationCustomerProductBll();
            bll.BatchCustomerProduct(list);
        }

        protected void btnSaveQuit_Click(object sender, EventArgs e)
        {
            List<RelationCustomerProductEntity> list = new List<RelationCustomerProductEntity>();
            RelationCustomerProductEntity entity = null;
            foreach (GridViewRow item in gvProductList.Rows)
            {
                CheckBox cbSelect = item.FindControl("cbSelect") as CheckBox;
                if (cbSelect != null)
                {
                    if (cbSelect.Checked)
                    {
                        entity = new RelationCustomerProductEntity();
                        entity.CustomerID = CustomerID;
                        entity.ProductID = new Guid(gvProductList.DataKeys[item.RowIndex]["ProductID"].ToString());
                        entity.Deleted = false;

                        list.Add(entity);
                    }
                }
            }

            RelationCustomerProductBll bll = new RelationCustomerProductBll();
            bll.BatchCustomerProduct(list);
            ClientScript.RegisterStartupScript(this.GetType(), "SaveAndClose", "alert('保存成功.');parent.$.ligerDialog.hide();parent.$('#btnRefersh').click();", true);

        }
    }
}